package com.cc.controller;

import com.cc.entity.Book;
import com.cc.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    BookService bookService;

    //获取全部书籍  作者/书籍名称
    @GetMapping
   public List<Book> getLists(String bookName,String author){

       return bookService.getList(bookName,author);
   }
    //获取书籍详情
    @GetMapping("{id}")
    public Book getBookInfo(@PathVariable("id") Integer BookId){

        return bookService.getBookInfo(BookId);
    }

    //新增书籍
    @PostMapping
    public String saveBook(@RequestBody Book book){
        int count = bookService.saveBook(book);
        if (count>0){
            return "新增书成功";
        }
        return "新增书失败";
    }

    //修改书籍
    @PutMapping
    public String updateBook(Book book){
        int count = bookService.updateBook(book);
        if (count>0){
            return "修改成功";
        }
        return "修改失败";
    }
    //删除书籍
    @DeleteMapping("{BookId}")
    public String deleteBook(@PathVariable Integer BookId){
        int count = bookService.deleteBook(BookId);
        if (count>0){
            return "删除成功";
        }
        return "删除失败";
    }

}
